/*
 *  Copyright (c) 2014 ZengYajian
 *
 *  Simple http protocol parser module.
 *  by ZengYajian
 */

#define _GNU_SOURCE
#include <stdio.h>

#include <string.h>
#include <stdlib.h>

#include "filter.h"
#include "log/log.h"

struct extensions {
	char *ext;
	char *filetype;
};

const struct extensions ext2type[] = {
	{ "gif",  "image/gif"  },
	{ "jpg",  "image/jpg"  },
	{ "jpeg", "image/jpeg" },
	{ "png",  "image/png"  },
	{ "ico",  "image/ico"  },
	{ "zip",  "image/zip"  },
	{ "gz",   "image/gz"   },
	{ "tar",  "image/tar"  },
	{ "htm",  "text/html"  },
	{ "html", "text/html"  },
	{NULL, NULL}
};

void resource_remap(const char *res, char *path, char *type)
{
	char *p;
	int i;

	if (!strcmp(res, "./")) {
		sprintf(path, "%s", "./index.html");
		sprintf(type, "%s", "text/html");
		return;
	}

	p = strstr(res+1, ".");
	if (!p) goto not_found;

	p++;
	for (i=0; ext2type[i].ext!=0; i++) {
		if (!strcmp(ext2type[i].ext, p)) {
			sprintf(path, "%s", res);
			sprintf(type, "%s", ext2type[i].filetype);
			return;
		}
	}

not_found:
	sprintf(path, "%s", "./404.html");
	sprintf(type, "%s", "text/html");
}
